package com.example.demo.service.impl;

import com.example.demo.Mapper.BookMapper;
import com.example.demo.model.BookExample;
import com.example.demo.model.Book;
import com.example.demo.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl implements BookService {
    

    @Autowired
    private BookMapper bookMapper;
    
    @Override
    public List<Book> selectByCondition(Integer bookinfoId, String bookname, String authorname, String press, String introduction, String isbn, Double price, String deleteFlag) {
        BookExample bookExample=new BookExample();
        BookExample.Criteria criteria=bookExample.createCriteria();

        if (bookinfoId!=null)
            criteria.andBookinfoIdEqualTo(bookinfoId);
        if(press!=null)
            criteria.andPressLike("%"+press+"%");
        if (bookname!=null)
            criteria.andBooknameLike("%"+bookname+"%");
        if (authorname!=null)
            criteria.andAuthornameLike("%"+authorname+"%");
        if (introduction!=null)
            criteria.andIntroductionEqualTo(introduction);
        if (isbn!=null)
            criteria.andIsbnLike("%"+isbn+"%");
        if (price!=null)
            criteria.andPriceEqualTo(price);
        if (deleteFlag!=null)
            criteria.andDeleteFlagEqualTo(deleteFlag);
        else
            criteria.andDeleteFlagEqualTo("0");
        return bookMapper.selectByExample(bookExample);
    }

    @Override
    public int bookUpdate(Book book) {
        return bookMapper.updateByPrimaryKeySelective(book);
    }

    @Override
    public int bookDelete(Book book) {
        book.setDeleteFlag("1");
        return bookMapper.updateByPrimaryKeySelective(book);
    }

    @Override
    public int bookInsert(Book book) {
        return bookMapper.insertSelective(book);
    }
}
